package com.example.serverbase.initdatabase

import com.example.serverbase.initdatabase.entity.User
import com.example.serverbase.initdatabase.repository.AddressBookRepository
import com.example.serverbase.initdatabase.repository.CategoryRepository
import com.example.serverbase.initdatabase.repository.DishFlavorRepository
import com.example.serverbase.initdatabase.repository.DishRepository
import com.example.serverbase.initdatabase.repository.EmployeeRepository
import com.example.serverbase.initdatabase.repository.OrdersRepository
import com.example.serverbase.initdatabase.repository.SetmealDishRepository
import com.example.serverbase.initdatabase.repository.SetmealRepository
import com.example.serverbase.initdatabase.repository.ShoppingCartRepository
import com.example.serverbase.initdatabase.repository.UserRepository
import org.springframework.boot.CommandLineRunner
import org.springframework.stereotype.Component
import java.math.BigDecimal
import java.time.LocalDateTime

@Component
class DataInitializer(
    private val userRepository: UserRepository,
    private val dishRepository: DishRepository,
    private val ordersRepository: OrdersRepository,
    private val addressBookRepository: AddressBookRepository,
    private val categoryRepository: CategoryRepository,
    private val setmealRepository: SetmealRepository,
    private val setmealDishRepository: SetmealDishRepository,
    private val shoppingCartRepository: ShoppingCartRepository,
    private val employeeRepository: EmployeeRepository,
    private val dishFlavorRepository: DishFlavorRepository
) : CommandLineRunner {

    override fun run(vararg args: String?) {
        // 插入用户数据
        val user1 = User(
            name = "Alice",
            phone = "1234567890",
            openid = "openid1",
            sex = "女",
            idNumber = "123456789012345678",
            avatar = "http://example.com/avatar1.jpg",
            createTime = LocalDateTime.now()
        )
        userRepository.save(user1)

        val user2 = User(
            name = "Bob",
            phone = "0987654321",
            openid = "openid2",
            sex = "男",
            idNumber = "098765432109876543",
            avatar = "http://example.com/avatar2.jpg",
            createTime = LocalDateTime.now()
        )
        userRepository.save(user2)

        // 插入菜品分类
        val category1 = Category(
            type = 1,
            name = "主食",
            sort = 1,
            status = 1,
            createTime = LocalDateTime.now(),
            updateTime = LocalDateTime.now(),
            createUser = user1.id,
            updateUser = user1.id
        )
        categoryRepository.save(category1)

        val category2 = Category(
            type = 2,
            name = "小吃",
            sort = 2,
            status = 1,
            createTime = LocalDateTime.now(),
            updateTime = LocalDateTime.now(),
            createUser = user1.id,
            updateUser = user1.id
        )
        categoryRepository.save(category2)

        // 插入菜品
        val dish1 = Dish(
            name = "宫保鸡丁",
            categoryId = category1.id,
            price = BigDecimal("20.00"),
            image = "http://example.com/dish1.jpg",
            description = "经典的川菜",
            status = 1,
            createTime = LocalDateTime.now(),
            updateTime = LocalDateTime.now(),
            createUser = user1.id,
            updateUser = user1.id
        )
        dishRepository.save(dish1)

        val dish2 = Dish(
            name = "鱼香肉丝",
            categoryId = category1.id,
            price = BigDecimal("18.00"),
            image = "http://example.com/dish2.jpg",
            description = "经典的川菜",
            status = 1,
            createTime = LocalDateTime.now(),
            updateTime = LocalDateTime.now(),
            createUser = user1.id,
            updateUser = user1.id
        )
        dishRepository.save(dish2)

        // 插入套餐
        val setmeal1 = Setmeal(
            name = "套餐A",
            categoryId = category1.id,
            price = BigDecimal("50.00"),
            image = "http://example.com/setmeal1.jpg",
            description = "包含宫保鸡丁和鱼香肉丝",
            status = 1,
            createTime = LocalDateTime.now(),
            updateTime = LocalDateTime.now(),
            createUser = user1.id,
            updateUser = user1.id
        )
        setmealRepository.save(setmeal1)

        // 插入套餐菜品关系
        val setmealDish1 = SetmealDish(
            setmealId = setmeal1.id,
            dishId = dish1.id,
            name = dish1.name,
            price = dish1.price,
            copies = 1
        )
        setmealDishRepository.save(setmealDish1)

        val setmealDish2 = SetmealDish(
            setmealId = setmeal1.id,
            dishId = dish2.id,
            name = dish2.name,
            price = dish2.price,
            copies = 1
        )
        setmealDishRepository.save(setmealDish2)

        // 插入购物车
        val shoppingCart1 = ShoppingCart(
            userId = user1.id,
            dishId = dish1.id,
            number = 2,
            amount = dish1.price?.multiply(BigDecimal(2)),
            createTime = LocalDateTime.now()
        )
        shoppingCartRepository.save(shoppingCart1)

        // 插入员工
        val employee1 = Employee(
            name = "John",
            username = "john_doe",
            phone = "1234567890",
            sex = "男",
            idNumber = "123456789012345678",
            createTime = LocalDateTime.now(),
            updateTime = LocalDateTime.now(),
            status = 1,
            password = "password123"
        )
        employeeRepository.save(employee1)

        // 插入菜品口味
        val dishFlavor1 = DishFlavor(
            dishId = dish1.id,
            name = "微辣",
            value = "1"
        )
        dishFlavorRepository.save(dishFlavor1)

        val dishFlavor2 = DishFlavor(
            dishId = dish1.id,
            name = "中辣",
            value = "2"
        )
        dishFlavorRepository.save(dishFlavor2)


        // 插入地址簿
        val addressBook1 = AddressBook(
            userId = user1.id,
            consigneeId = 1,
            phone = "1234567890",
            sex = "女",
            provinceCode = "110000",
            provinceName = "北京市",
            cityCode = "110100",
            cityName = "北京市",
            districtCode = "110105",
            districtName = "海淀区",
            detail = "中关村",
            label = "家",
            isDefault = 1
        )
        addressBookRepository.save(addressBook1)

        val addressBook2 = AddressBook(
            userId = user2.id,
            consigneeId = 2,
            phone = "0987654321",
            sex = "男",
            provinceCode = "310000",
            provinceName = "上海市",
            cityCode = "310100",
            cityName = "上海市",
            districtCode = "310115",
            districtName = "浦东新区",
            detail = "陆家嘴",
            label = "公司",
            isDefault = 1
        )
        addressBookRepository.save(addressBook2)


        // 插入订单
        val order1 = Orders(
            number = "20230520001",
            status = 1,
            userId = user1.id,
            addressBookId = 1L,
            payStatus = 1,
            payMethod = 1,
            orderTime = LocalDateTime.now(),
            checkoutTime = LocalDateTime.now(),
            amount = BigDecimal("38.00"),
            remark = "无",
            userName = user1.name,
            phone = user1.phone,
            address = "北京市海淀区",
            consignee = "Alice",
            cancelReason = null,
            rejectionReason = null,
            cancelTime = null,
            estimatedDeliveryTime = LocalDateTime.now().plusHours(1),
            deliveryStatus = 1,
            deliveryTime = LocalDateTime.now().plusHours(2),
            packAmount = 2,
            tablewareNumber = 2,
            tablewareStatus = 1
        )
        ordersRepository.save(order1)

        val order2 = Orders(
            number = "20230520002",
            status = 2,
            userId = user2.id,
            addressBookId = 2L,
            payStatus = 1,
            payMethod = 2,
            orderTime = LocalDateTime.now(),
            checkoutTime = LocalDateTime.now(),
            amount = BigDecimal("36.00"),
            remark = "无",
            userName = user2.name,
            phone = user2.phone,
            address = "上海市浦东新区",
            consignee = "Bob",
            cancelReason = null,
            rejectionReason = null,
            cancelTime = null,
            estimatedDeliveryTime = LocalDateTime.now().plusHours(1),
            deliveryStatus = 1,
            deliveryTime = LocalDateTime.now().plusHours(2),
            packAmount = 2,
            tablewareNumber = 2,
            tablewareStatus = 1
        )
        ordersRepository.save(order2)
    }
}